import wave
import datetime
from os.path import join, dirname

def transfer(pcm_path = 'assets/1718263328710.pcm', wav_path = 'assets/test.wav', channels=1, sample_width=2, frame_rate=11025):
    """
    将一个PCM文件转换为WAV文件。

    :param pcm_path: PCM文件的路径
    :param wav_path: 输出的WAV文件的路径
    :param channels: 声道数（默认为1）
    :param sample_width: 采样深度（默认为16 bit，即2 bytes）
    :param frame_rate: 采样率（默认为11025 Hz）
    """
    pcm_path = join(dirname(__file__), pcm_path)
    wav_path = join(dirname(__file__), wav_path)
    start = datetime.datetime.now()
    with open(pcm_path, 'rb') as pcmf:
        pcmdata = pcmf.read()
        pcmf.close()
    with wave.open(wav_path, 'wb') as wavfile:
        wavfile.setnchannels(channels)
        wavfile.setsampwidth(sample_width)
        wavfile.setframerate(frame_rate)
        wavfile.writeframes(pcmdata)
        wavfile.close()

    end = datetime.datetime.now()
    return str((end - start).seconds)
if __name__ == '__main__':
    pcm_path ="./assets/1718262771601.pcm"
    transfer(pcm_path,'./test_wav.wav')